package com.spzx.order.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.order.domain.vo.OrderTradeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.service.IOrderInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author atguigu
 * @date 2024-07-12
 */
@Tag(name = "订单接口管理")
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController
{
    @Autowired
    private IOrderInfoService orderInfoService;
    @Operation(summary = "内部调用：确认支付获取订单信息")
    @InnerAuth
    @GetMapping("getPaymentOrderInfo/{orderNo}")
    public R<OrderInfo> getOrderInfoByOrderNo(@PathVariable("orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        return R.ok(orderInfo);
    }
    @Operation(summary = "取消订单")
    @RequiresLogin
    @GetMapping("cancelOrder/{orderId}")
    public AjaxResult cancelOrder(@PathVariable Long orderId){
        return toAjax(orderInfoService.cancelOrder(orderId));
    }
    ///orderInfo/buy/3
    @Operation(summary = "立即购买")
    @RequiresLogin
    @GetMapping("buy/{skuId}")
    public AjaxResult buy(@PathVariable Long skuId) {
        OrderTradeVO orderTradeVO = orderInfoService.buy(skuId);
        return success(orderTradeVO);
    }
    @Operation(summary = "获取订单信息")
    @RequiresLogin
    @GetMapping("getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderInfoService.selectOrderInfoById(orderId);
        return success(orderInfo);
    }
    @Operation(summary = "查询用户的订单列表")
    @GetMapping("userOrderInfoList/{pageNum}/{pageSize}")
    public TableDataInfo userOrderInfoList(@PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize,
                                      @RequestParam(value = "orderStatus" ,required = false )Integer orderStatus){
        return getDataTable(orderInfoService.userOrderInfoList(pageNum,pageSize,orderStatus));
    }
    //orderInfo/getOrderInfo/f72ee20592e642d0827a747e61549a03
//    @Operation(summary = "查询订单详情")
//    @GetMapping("getOrderInfo/{orderNo}")
//    public AjaxResult getOrderInfo(@PathVariable("orderNo") String orderNo){
//        return success(orderInfoService.getOne(Wrappers.lambdaQuery(OrderInfo.class)
//                .eq(OrderInfo::getOrderNo , orderNo)));
//    }

    // trade: 去结算接口
    //  查询当前用户购物车中选中的购物项转为页面需要的数据OrderItem  展示
    // 远程访问购物车查询选中的购物项
    @Operation(summary = "结算接口:不会持久化订单数据")
    @GetMapping("trade")
    public AjaxResult trade(){
        OrderTradeVO orderTradeVO = orderInfoService.trade();
        return success(orderTradeVO);
    }
    //orderInfo/submitOrder
    @Operation(summary = "提交订单接口:保存订单数据")
    @PostMapping("submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderInfo orderInfo){
        // 订单id和订单编号 都表示唯一的订单
        String orderId = orderInfoService.submitOrder(orderInfo);
        return AjaxResult.success("订单创建成功",orderId);
    }
}
